using System;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Configuration;


namespace CCSService_WebRole
{
	/// <summary>
	/// Implements the client code for preparing a license request
	/// </summary>
	public class LicenseRequest : License
	{
		/// <summary>
		/// Default constructor
		/// </summary>
		public LicenseRequest()
		{
		}

		/// <summary>
		/// Initialization of a license request 
		/// </summary>
		/// <param name="product">Product name string</param>
		/// <param name="licenseLevel">License level to request</param>
		/// <param name="clientIdentity">Unique client identity</param>
		public void Initialize(
			string product,
			string licenseLevel,
			string clientIdentity)
		{
            this.ClientIdentity = clientIdentity;
            this.LicenseLevel = licenseLevel;
            this.Product = product;
			//ComputeClientIdentity(licenseLevel, product, clientIdentity);
		}
	}

	/// <summary>
	/// Implements the client code for requesting a license, and 
	/// validating a license once it's been received from the server.
	/// </summary>
	public class LicenseClient
	{
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="product">Product name string</param>
		/// <param name="serverPublicKey">Server public key</param>
		/// <param name="clientIdentity">Unique client identity</param>
		public LicenseClient(
			string product, 
			string serverPublicKey,
			string clientIdentity)
		{
			m_Product = product;
			m_ServerPublicKey = serverPublicKey;
			m_ClientIdentity = clientIdentity;
		}

		/// <summary>
		/// Prepare a license request for the specified product at the 
		/// specified feature level 
		/// </summary>
		/// <param name="licenseLevel">License level to request</param>
		/// <returns>License request object</returns>
		public LicenseRequest CreateLicenseRequest(
			string licenseLevel)
		{
			LicenseRequest request = new LicenseRequest();
			request.Initialize(m_Product, licenseLevel, m_ClientIdentity);
			
			return request;
		}

		/// <summary>
		/// Verify that the response received from the license server is valid 
		/// </summary>
		/// <param name="response">License response object</param>
		/// <returns>Authenticated license level</returns>
		public string VerifyLicense(LicenseResponse response)
		{
			return response.VerifyLicenseResponse(m_ServerPublicKey);			
		}

		private string m_Product;
		private string m_ServerPublicKey;
		private string m_ClientIdentity;
	}
}
